package Classes.GameViews.ViewPanels
{
	import Classes.GameDatas.GD_Item;
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GD_Shop;
	import Classes.GameUtils.GU_MsgDeal;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MMT_Character;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_Zombie;
	import Classes.GameViews.ViewScenes.VS_Main;
	import Classes.Interfaces.I_SendTarget;
	import Classes.Managers.M_Communication;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.Language;
	import Classes.staticinfo.PlatformInfo;
	
	import MyComponent.MyButton;
	
	import UIComponents.IGD_ShopItem;
	import UIComponents.PanelEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.system.ApplicationDomain;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 *快速购买类 
	 * @author 邓坚
	 */
	public class VP_DiePanel extends ViewPanel  implements I_SendTarget
	{
		private static var _instance:VP_DiePanel;
		private var _interfaceUI:Sprite;
		private var _shopItem1:IGD_ShopItem;
		private var _shopItem2:IGD_ShopItem;
		private var _nowShop:IGD_ShopItem;
		public function VP_DiePanel()
		{
			init();
		}
		
		public function init():void
		{
			_interfaceUI	=  	new (ApplicationDomain.currentDomain.getDefinition("FastBuy_diePanel_v") as Class);
			this.addChild(_interfaceUI);
			addListener();
		}
		
		public function addListener():void
		{
			this.addEventListener(PanelEvent.CLOSE,closeFun);
			this.addEventListener("usebtn1Click",usebtn1Click);
			this.addEventListener("usebtn2Click",usebtn2Click);
			this.addEventListener("btnC1Click", btnC1Click);
			this.addEventListener("btnC2Click", btnC2Click);
		}
		
		/**
		 *使用左边 
		 * @param e
		 * 
		 */
		private function usebtn1Click(e:Event):void
		{
//			this._nowShop	=	this._shopItem1;
//			
//			var msgContent:Object={itemID:this._nowShop.shopItemId,friendId:0};
//			M_Communication.instance.send(12303,msgContent);
			
			var mt:MMT_Character=GV_Map.instance.controlTarget as MMT_Character;
			mt.mapGrid=GV_Map.instance.getRandomGridForcharacter(mt.characterData);
			mt.setPosition(GV_Map.instance.getRandomGridForcharacter(mt.characterData));
			GD_Player.instance.tweenCenter();
			
			this.closeFun(null);
//			
//			if(isClose)
//				this.closeFun(null);
		}
		
		/**
		 *使用右边 
		 * @param e
		 * 
		 */
		private function usebtn2Click(e:Event):void
		{
			this._nowShop	=	this._shopItem2;
			var msgContent:Object={itemID:this._nowShop.shopItemId,friendId:0};
			M_Communication.instance.send(12303,msgContent);
			
			this.closeFun(null);
			
		}
		
		/**
		 *购买左边 
		 * @param e
		 * 
		 */
		private function btnC1Click(e:Event):void
		{
			this._nowShop	=	this._shopItem1;
			
			if(M_ConfigData.instance.isOpenQQVip && this._nowShop.moneyType==0)
			{
				PlatformInfo.instance.qqPlatBuyItemFun(this._nowShop.shopItemType,this._nowShop.shopItemImg,1,(_nowShop.itemName+"*"+_nowShop.itemName),_nowShop.shopItemId);
				
			}else{
					if(!this._shopItem1.isReachMoney){		//没有足够的钱
						VP_errorPanel.instance.setErrorMessage("没有足够的钱");
						VP_errorPanel.instance.enterFrame();
						GU_Sound.instance.playCommSound("System_Prompt");
						this.closeFun(new Event(""));
						return;
					}
					
					if(this._shopItem1){
						var msgContent1:Object={itemType:this._shopItem1.shopItemType,pil:1};
						M_Communication.instance.send(12100,msgContent1,this);
						this.closeFun(new Event(""));
					}
			}
		}
		
		/**
		 *购买右边 
		 * @param e
		 * 
		 */
		private function btnC2Click(e:Event):void
		{
			this._nowShop	=	this._shopItem2;
			
			if(M_ConfigData.instance.isOpenQQVip && this._nowShop.moneyType==0)
			{
				PlatformInfo.instance.qqPlatBuyItemFun(this._nowShop.shopItemType,this._nowShop.shopItemImg,1,(_nowShop.itemName+"*"+_nowShop.itemName),_nowShop.shopItemId);
				
			}else{
				
				if(!this._nowShop.isReachMoney){		//没有足够的钱
					VP_errorPanel.instance.setErrorMessage("没有足够的钱");
					VP_errorPanel.instance.enterFrame();
					GU_Sound.instance.playCommSound("System_Prompt");
					this.closeFun(new Event(""));
					return;
				}
				
				if(this._nowShop){
					var msgContent1:Object={itemType:this._nowShop.shopItemType,pil:1};
					M_Communication.instance.send(12100,msgContent1,this);
					this.closeFun(new Event(""));
				}
			}
			
		}
		
		private function closeFun(e:Event):void
		{
			GV_Map.instance.buildMode	=	GV_Map.CANNOTBUILDMODE;
			this.close();
		}
		
		public static function get instance():VP_DiePanel
		{
			if(!_instance) _instance=new VP_DiePanel;
			return _instance;
		}
		
		public function get interfaceUI():Sprite
		{
			return _interfaceUI;
		}
		
		public function openPanel():void
		{
			var mtype:String;
			if(M_ConfigData.instance.isOpenQQVip)
				mtype	=	"qb";
			else
				mtype	=	"ob";
			this._interfaceUI["setMoenyIcon1"](mtype);
			this._interfaceUI["itmePrice1"]=100;
			
			//			if( ( (GV_Map.instance.controlTarget as MMT_Character).actTarget is MN_Zombie ) )
			//			if( ((GV_Map.instance.controlTarget as MMT_Character).actTarget as MN_Zombie ).actionName!="die")
			GV_Map.instance.buildMode	=	GV_Map.MOVEMODE;
			this.openAt(VS_Main.instance,true);
			this.toCenter();
			
			VP_DiePanel.instance.setShopRight(10320001);
			VP_DiePanel.instance.setShopLeft(10320002);
		}
		
		
		public function setShopLeft(itemId:uint):void
		{
			var tipString:String	=	"";
			
			_shopItem1=GD_Shop.instance.getShopInfoByItemInfo(itemId);
			if(_shopItem1==null){
				this.closeFun(null);
				return;
			}
			this._interfaceUI["spPic1"]=_shopItem1.shopItemPic;
			
			var isHave:Boolean	=	false;
			for each(var item:GD_Item in GD_Player.instance.items)
			{
				if(item.itemId	== itemId){isHave=true;break;} 
			}

			isHave =	true;
			
			if(isHave){
																	//热感
				tipString	=	Language.DiePanelLeftTipUse;
				setTip(this.interfaceUI["spPic1"] as DisplayObject, tipString);
						
				
				
				this._interfaceUI["btnContainerLeft"].visible=false;
				(this._interfaceUI["useBtnLeft"] as MyButton).visible=true;
				//				this._interfaceUI.addChild( (this._interfaceUI.getChildByName("btn1") as MyButton));
			}
			else{
				
				//热感
				tipString	=	Language.DiePanelLeftTipBuy;
				tipString	=	tipString.replace( "@JbType", ( this._shopItem1.moneyType==0?"欧币":"金币"));
				tipString	=	tipString.replace( "@JbNum", this._shopItem1.price);
				tipString	=	tipString.replace( "@Title", this._shopItem1.itemName);
				tipString	=	tipString.replace( "@Num", this._shopItem1.perNum);	
					
				setTip(this.interfaceUI["spPic1"] as DisplayObject, tipString);
				
				
				this._interfaceUI["btnContainerLeft"].visible=true;
				(this._interfaceUI["useBtnLeft"] as MyButton).visible=false;
				
				(this._interfaceUI["buyBtnLeft"] as MyButton).buttonMode	 = (this._interfaceUI["btnContainerLeft"] as DisplayObjectContainer).mouseEnabled 	=	this._shopItem1.isReachMoney;
				
				var mtype:String="jb";
				if(this._shopItem1.moneyType==0){
					if(M_ConfigData.instance.isOpenQQVip)
						mtype	=	"qb";
					else
						mtype	=	"ob";
				}
				this._interfaceUI["setMoenyIcon1"](mtype);
				this._interfaceUI["itemPrice1"]=this._shopItem1.price;
			}
			
		}
		
		
		public function setShopRight(itemId:uint):void
		{
			var tipString:String	=	"";
			
			_shopItem2=GD_Shop.instance.getShopInfoByItemInfo(itemId);
			if(_shopItem2==null){
				this.closeFun(null);
				return;
			}
			this._interfaceUI["spPic2"]=_shopItem2.shopItemPic;
			
			//tf.mouseEnabled	=	_txt2.mouseEnabled	=	false;
			
			var isHave:Boolean	=	false;
			if( GD_Player.instance.items.hasOwnProperty( itemId) && (GD_Player.instance.items[itemId] as GD_Item).itemNumber>0)isHave=true;
//			for each(var item:GD_Item in GD_Player.instance.items)
//			{
//				if(item.itemId	== itemId){isHave=true;break;} 
//			}
			
			if(isHave){
				//热感
				tipString	=	Language.DiePanelRightTipUse;
				setTip(this.interfaceUI["spPic2"] as DisplayObject, tipString);
				
				this._interfaceUI["btnContainerRight"].visible=false;
				(this._interfaceUI["useBtnRight"] as MyButton).visible=true;
				//				this._interfaceUI.addChild( (this._interfaceUI.getChildByName("btn1") as MyButton));
			}
			else{
				
				//热感
				tipString	=	Language.DiePanelRightTipBuy;
				tipString	=	tipString.replace( "@JbType", ( this._shopItem2.moneyType==0?"欧币":"金币"));
				tipString	=	tipString.replace( "@JbNum", this._shopItem2.price);
				tipString	=	tipString.replace( "@Title", this._shopItem2.itemName);
				tipString	=	tipString.replace( "@Num", this._shopItem2.perNum);	
				
				setTip(this.interfaceUI["spPic2"] as DisplayObject, tipString);
				
				this._interfaceUI["btnContainerRight"].visible=true;
				(this._interfaceUI["useBtnRight"] as MyButton).visible=false;
				
				(this._interfaceUI["buyBtnRight"] as MyButton).buttonMode	 = (this._interfaceUI["btnContainerRight"] as DisplayObjectContainer).mouseEnabled 	=	this._shopItem2.isReachMoney;
				
				var mtype:String="jb";
				if(this._shopItem2.moneyType==0){
					if(M_ConfigData.instance.isOpenQQVip)
						mtype	=	"qb";
					else
						mtype	=	"ob";
				}
				this._interfaceUI["setMoenyIcon2"](mtype);
				this._interfaceUI["itemPrice2"]=this._shopItem2.price;
			}
		}
		
		
		
		/**
		 *发送一个消息,返回的时候拦截 
		 * @param msgObje
		 * 
		 */
		public function dealMsg(dealObj:Object):void
		{		
			if(dealObj.msgType==22600)			//更改道具数量
			{
				var msgContent:Object={itemID:this._nowShop.shopItemId,friendId:0};
				M_Communication.instance.send(12303,msgContent);
			}
			
			(GU_MsgDeal.instance["dealMsg_"+dealObj.msgType] as Function)(dealObj);
		}
		
		/**
		 *设置热感 
		 * @param d
		 * @param st
		 * 
		 */
		public function setTip(d:DisplayObject, st:String):void
		{
			ToolTip_Sprite.getInstance().removeTip(d);
			ToolTip_Sprite.getInstance().setTip(d,new MyTipSprite(st),ToolTip_Sprite.DIRECTION_LEFT);
		}
		
	}
}